<?php

/**
 * CondoritoEstudiante form base class.
 *
 * @method CondoritoEstudiante getObject() Returns the current form's model object
 *
 * @package    condorito
 * @subpackage form
 * @author     Freddy Daniel Garcia
 */
abstract class BaseCondoritoEstudianteForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                                 => new sfWidgetFormInputHidden(),
      'codigo'                             => new sfWidgetFormInputText(),
      'primer_nombre'                      => new sfWidgetFormInputText(),
      'primer_apellido'                    => new sfWidgetFormInputText(),
      'semestre'                           => new sfWidgetFormInputText(),
      'proyecto_curricular'                => new sfWidgetFormInputText(),
      'tipo_identificacion'                => new sfWidgetFormInputText(),
      'identificacion'                     => new sfWidgetFormInputText(),
      'correo_personal'                    => new sfWidgetFormInputText(),
      'id_usuario'                         => new sfWidgetFormInputHidden(),
      'condorito_estudiante_inscrita_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'CondoritoMateria')),
    ));

    $this->setValidators(array(
      'id'                                 => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
      'codigo'                             => new sfValidatorString(array('max_length' => 255)),
      'primer_nombre'                      => new sfValidatorString(array('max_length' => 255)),
      'primer_apellido'                    => new sfValidatorString(array('max_length' => 255)),
      'semestre'                           => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'proyecto_curricular'                => new sfValidatorString(array('max_length' => 255)),
      'tipo_identificacion'                => new sfValidatorString(array('max_length' => 255)),
      'identificacion'                     => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'correo_personal'                    => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'id_usuario'                         => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'condorito_estudiante_inscrita_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'CondoritoMateria', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorPropelUnique(array('model' => 'CondoritoEstudiante', 'column' => array('id_usuario')))
    );

    $this->widgetSchema->setNameFormat('condorito_estudiante[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'CondoritoEstudiante';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['condorito_estudiante_inscrita_list']))
    {
      $values = array();
      foreach ($this->object->getCondoritoEstudianteInscritas() as $obj)
      {
        $values[] = $obj->getIdMateria();
      }

      $this->setDefault('condorito_estudiante_inscrita_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveCondoritoEstudianteInscritaList($con);
  }

  public function saveCondoritoEstudianteInscritaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['condorito_estudiante_inscrita_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(CondoritoEstudianteInscritaPeer::ID_ESTUDIANTE, $this->object->getPrimaryKey());
    CondoritoEstudianteInscritaPeer::doDelete($c, $con);

    $values = $this->getValue('condorito_estudiante_inscrita_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new CondoritoEstudianteInscrita();
        $obj->setIdEstudiante($this->object->getPrimaryKey());
        $obj->setIdMateria($value);
        $obj->save();
      }
    }
  }

}
